<!-- 菜单悬浮的原理: 通过给菜单添加position:sticky实现, 用法超简单, 仅APP端的低端机不兼容 https://caniuse.com/#feat=css-sticky -->
<template>
	<view>
		<!-- 对于mescroll-body: 需设置:sticky="true", 此应避免在mescroll-body标签前面加其他非定位的元素, 否则下拉区域会被挤出, 无法会隐藏.-->
		<!-- 对于mescroll-uni: 则无需设置:sticky="true", 无其他限制和要求 -->
		<!-- sticky吸顶悬浮的菜单, 父元素必须是 mescroll -->
		<view class="sticky-tabs">
			<!-- <me-tabs v-model="tabIndex" nameKey='title' :tabs="tabs" @change="tabChange"></me-tabs> -->
			<view class="padding-tb-sm bg">
				<view class="tui-tabs" style="width: 100%;">
					<scroll-view id="tab-bar" scroll-with-animation class="tui-scroll-h" :scroll-x="true"
						:show-scrollbar="false">
						<view style="display: flex;width: 100%;white-space: nowrap;overflow-x: auto;">
							<view :key='index' v-for="(item, index) in tabs"
								style="width:30%;margin-left: 16upx;text-align: center" :id="item.id"
								:data-current="index" @tap="tabChange(item)">
								<view class="nameclore" :class="{ 'tui-tab-item-title-active': tabIndex == item.id }">
									{{ item.title }}
								</view>
								<view class="weix" v-if="tabIndex == item.id">
									<image src="../../static/images/index/indexbottom.png"></image>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
		<mescroll-body :sticky="true" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<!-- 数据列表 -->
			<view v-for="(item,index) in goods" :key='index' @click="bindtakeDetail(item.helpOrderId)">
				<view class="bg margin-lr radius margin-top-sm">
					<view class="flex justify-between padding-lr padding-top-sm">
						<view>{{item.createTime}}</view>
						<view style="color: #557EFD;" v-if="item.status == 1">进行中</view>
						<view style="color: #557EFD;" v-if="item.status == 2">已完成</view>
						<view style="color: #557EFD;" v-if="item.status == 3">已取消</view>
					</view>
					<view style="border-top: 1rpx solid #F2F2F2;margin-top: 20upx;"></view>
					<view class="flex justify-between text-lg text-bold padding-lr padding-tb-sm">
						<view>{{item.serviceName}}</view>
						<view style="color: #557EFD;">赏金￥{{item.money}}</view>
					</view>
					<view class="padding-lr">
						<view class="flex text-sm" style="color: #999999;">服务地址：<view>{{item.detailsAddress}}</view>
						</view>
						<view class="flex text-sm margin-top-sm" style="color: #999999;">预约时间：<view>
								{{item.deliveryTime}}
							</view>
						</view>
					</view>
					<view style="border-top: 1rpx solid #F2F2F2;margin-top: 30upx;"></view>
					<view class="flex justify-between padding-lr padding-tb-sm align-center">
						<view class="flex align-center" style="color: #999999;">
							赏金:<text class="text-bold text-lg" style="color: #000000;">￥{{item.money}}</text>
						</view>
						<view class="flex">
							<view class="btn1" @click.stop="cancelOrder(item)" v-if="item.status ==1">取消订单</view>
							<!-- <view class="btn1" @click.stop="detelOrder(item)">删除订单</view> -->
							<view class="btn2" @click.stop="openCode(item)" v-if="item.status ==1">输入确认码</view>
						</view>
					</view>
				</view>
			</view>
			<empty v-if="goods.length == 0"></empty>
		</mescroll-body>

		<u-popup v-model="show" mode="center" width="500rpx" height="190px" border-radius="14" closeable="true">
			<view class="padding-lr" style="margin-top: 30upx;">
				<view class="text-lg text-bold margin-bottom">确认码</view>
				<view class=" padding-lr-sm" style="background: #F5F5F5;">
					<!-- <input type="number" v-memo="codes" placeholder="请输入确认码" @confirm="change()"/> -->
					<u-input v-model="codes" type="number" placeholder="请输入确认码" @confirm="change()" />
				</view>
				<view class="btn3" @click="sure()">确认</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import mescrollBody from "@/components/mescroll-uni/components/mescroll-body/mescroll-body.vue";
	import meTabs from "@/components/mescroll-uni/me-tabs/me-tabs.vue";
	import empty from '../../components/empty.vue'

	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			mescrollBody,
			meTabs,
			empty
		},
		data() {
			return {
				show: false,
				codes: '',
				goods: [], // 数据列表
				tabs: [{
						id: 0,
						title: '全部',
						status: ''
					}, {
						id: 1,
						title: '进行中',
						status: '1'
					}, {
						id: 2,
						title: '已完成',
						status: '2'
					},
					// {
					// 	id: 3,
					// 	title: '已取消',
					// 	status: '3'
					// }
				],
				tabIndex: 0, // tab下标
				game: [],
				page: 1,
				limit: 10,
				userId: 0,
				status: 1,
				nickName: '',
				avatar: '',
				customStyle: {
					color: '#999999',
					border: '2rpx solid #999999',
					// backgroundColor: '#1E1F31',
					border: "8rpx",
					width: '180rpx',
					height: '54rpx',
					margin: "0 0 0 20rpx"
				},
				customStyle1: {
					color: '#557EFD',
					border: '2rpx solid #557EFD',
					// backgroundColor: '#1E1F31',
					border: "8rpx",
					width: '180rpx',
					height: '54rpx',
					margin: "0 0 0 20rpx"
				},
				helpTakeId: '',
				helpOrderId: ''
			}
		},
		onLoad() {
			this.$queue.showLoading("加载中...");
			this.userId = uni.getStorageSync('userId')
			this.nickName = uni.getStorageSync('nickName')
		},
		onShow() {
			this.downCallback()
			this.upCallback(this.page)
		},
		methods: {
			// 接单详情
			bindtakeDetail(id) {
				console.log('8888888',id)
				uni.navigateTo({
					// url: './takeDetail?id=' + id
					url:'/my/takeOrder/takeDetail?id='+id
				})
			},
			/*下拉刷新的回调 */
			downCallback() {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				// loadSwiper();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let curTab = this.tabs[this.tabIndex].status

				let data = {
					status: curTab,
					page: page.num,
					limit: page.size
				}
				this.$Request.get('/app/help/selectRunHelpOrder', data).then(res => {
					uni.hideLoading();
					this.mescroll.endBySize(res.data.list.length, res.data.totalCount)
					if (page.num == 1) this.goods = []; //如果是第一页需手动制空列表
					res.data.list.forEach(ret => {
						switch (ret.state) {
							case 0:
								ret.statusName = '待接单'
								break;
							case 1:
								ret.statusName = '进行中'
								break;
							case 2:
								ret.statusName = '已完成'
								break;
							// case 3:
							// 	ret.statusName = '已拒绝'
							// 	break;
						}
					})
					this.goods = [...this.goods, ...res.data.list]; //追加新数据
					for (let i = 0; i < this.goods.length; i++) {
						this.goods[i].gameName = this.goods[i].gameName.split(",");
					}
					this.mescroll.endSuccess(res.data.total.length); // 隐藏加载状态栏

				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				});
			},
			// 切换菜单
			tabChange(tab) {
				this.tabIndex = tab.id
				this.goods = []; // 置空列表,显示加载进度条
				this.mescroll.resetUpScroll()
			},
			// 输入验证码
			openCode(e) {
				this.show = true
				this.helpTakeId = e.helpTakeId
				this.helpOrderId = e.helpOrderId
			},
			change(e) {
				this.codes = e
			},
			sure() {
				console.log(this.codes, '000000')
				let data = {
					helpTakeId: this.helpTakeId,
					helpOrderId: this.helpOrderId,
					code: this.codes
				}
				this.$Request.post('/app/help/closeOrder', data).then(res => {
					if (res.code == 0) {
						this.show = false
						this.codes = ''
						this.mescroll.resetUpScroll()
					}else {
						this.show = false
						this.codes = ''
						uni.showToast({
							title: '确认码不正确！',
							icon: 'none',
							duration: 1000
						})
					}
				})
			},
			// 取消订单
			cancelOrder(e) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定取消订单吗？',
					success: function(res) {
						if (res.confirm) {
							let data = {
								id: e.helpTakeId
							}
							that.$Request.post('/app/help/endHelpTake', data).then(res => {
								if (res.code == 0) {
									that.mescroll.resetUpScroll()
								}
							})
						}
					},
				})
			},
			clickItem: function(options) {
				let data = {
					userId: this.userId,
					focusedUserId: options.ordersUserId
				}
				this.$Request.postJson('/app/chat/insertChatConversation ', data).then(res => {
					if (res.code == 0) {
						let id = this.userId == res.data.userId ? res.data.focusedUserId : this.userId
						uni.navigateTo({
							url: '/pages/msg/im?chatConversationId=' + res.data.chatConversationId +
								'&byUserId=' + id
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/*
	sticky生效条件：
	1、父元素不能overflow:hidden或者overflow:auto属性。(mescroll-body设置:sticky="true"即可, mescroll-uni本身没有设置overflow)
	2、必须指定top、bottom、left、right4个值之一，否则只会处于相对定位
	3、父元素的高度不能低于sticky元素的高度
	4、sticky元素仅在其父元素内生效,所以父元素必须是 mescroll
	*/
	.sticky-tabs {
		z-index: 990;
		position: sticky;
		top: var(--window-top);
		// background-color: #fff;
	}

	// 使用mescroll-uni,则top为0
	.mescroll-uni,
	/deep/.mescroll-uni {
		.sticky-tabs {
			top: 0;
		}
	}

	.demo-tip {
		padding: 18upx;
		font-size: 24upx;
		text-align: center;
	}

	page {
		background-color: #F7F7F7;
	}

	.bg {
		background-color: #FFFFFF;
	}

	.tabber {
		width: 100%;
		background: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		justify-content: flex-end;
		height: 127rpx;
	}

	.weix image {
		width: 54upx;
		height: 15upx;
	}

	.nameclore {
		text-align: center;
		font-size: 28upx;
		padding: 0rpx 20rpx;
	}

	.nameclore.tui-tab-item-title-active {
		color: #5E81F9;
		font-size: 30upx;
	}

	.btn3 {
		background: #557EFD;
		border-radius: 16upx;
		text-align: center;
		color: #FFFFFF;
		padding: 20rpx 10rpx;
		margin-top: 60rpx;
	}

	.btn1 {
		background: #F5F5F5;
		color: #999999;
		padding: 10upx 25upx;
		border-radius: 40upx;
		margin-right: 20upx;
		// border: 1upx solid #666666;
	}

	.btn2 {
		background: #5E81F9;
		color: #FFFFFF;
		padding: 10upx 25upx;
		border-radius: 40upx;
	}
</style>
